home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Development Platforms / Apple II / Essentials / AppleScanGS / Control.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-04  |  8.2 KB  |  339 lines  |  [TEXT/MPS ]

  1. /**********************************************************************
  2. *
  3. * Control.c
  4. *
  5. * Copyright (c)  1990
  6. * Apple Computer, Inc.
  7. * All Rights Reserved.
  8. *
  9. * This file contains the code which implements the 
  10. * procedures for the various controls.
  11. *
  12. **********************************************************************/
  13. #define rulerXoffset 0
  14. #define rulerYoffset 0
  15. #define previewRes    75
  16. #define   maxX  10200
  17. #define   maxY  16800
  18. #define      yOrg 10
  19. #define   xOrg 36
  20.  
  21. #include <control.h>
  22. #include <intmath.h>
  23. #include <types.h>
  24. #include <list.h>
  25. #include <locator.h>
  26. #include <quickdraw.h>
  27. #include <qdaux.h>
  28. #include <event.h>
  29. #include <lineEdit.h>
  30. #include <menu.h>
  31. #include <memory.h>
  32. #include <resources.h>
  33. #include <window.h>
  34. #include "Scan.h"
  35. #include "Scanner_GSOS.h"
  36.  
  37. extern scannerWindowDef    sWindowDef;
  38. extern GrafPortPtr        previewWindowPtr;
  39. extern void updateScannerWindow();
  40. extern void updatePreviewWindow();
  41.  
  42. /* called by TrackControl when the mouse is held down in one of the scroll controls */
  43. pascal void scrollAction(partCode,ctrlHndl)
  44. int        partCode;
  45. Handle    ctrlHndl;
  46. {
  47.     int    currentValue;
  48.     
  49.     if(partCode) {    /* only do the work if the part code is valid */
  50.         currentValue = GetCtlValue(ctrlHndl);    /* get the current value */
  51.         switch(partCode) {    /* which part is active? */
  52.             case downArrow:    /* right arrow */
  53.                 if(++currentValue > 255)    /* make sure we stay within the value bounds */
  54.                     currentValue = 255;
  55.                 break;
  56.             case upArrow:    /* left arrow */
  57.                 if(--currentValue < 0)
  58.                     currentValue = 0;
  59.                 break;
  60.             case pageDown:    /* right paging region */
  61.                 if((currentValue += 8) > 255)
  62.                     currentValue = 255;
  63.                 break;
  64.             case pageUp:    /* left paging region */
  65.                 if((currentValue -= 8) < 0)
  66.                     currentValue = 0;
  67.                 break;
  68.         }
  69.         SetCtlValue(currentValue, ctrlHndl);
  70.     }
  71.     return;
  72. }
  73.  
  74. word getResolutionPopUp(theControlHandle)
  75. Handle    theControlHandle;
  76. {
  77.     static int resTable[] = {0, 75, 100, 150, 200, 300};
  78.  
  79.     return(resTable[GetCtlValue(theControlHandle)]);
  80. }
  81.  
  82. long doGetXYPos( xStart,xEnd,yStart,yEnd)
  83. unsigned long    *xStart,*xEnd,*yStart,*yEnd;
  84. {
  85.     CtlRecPtr        boxCtl;
  86.     Rect            *boxRect;
  87.     unsigned        i;
  88.     unsigned long    temp, deltaX;
  89.  
  90.     if(!previewWindowPtr) {    /* if preview window has not been opened... */
  91.         *xStart = *yStart = 0;    /* use default values to define page */
  92.         *xEnd = 10200;    /* 1200 * 8.5 inches */
  93.         *yEnd = 13200;    /* 1200 * 11 inches */
  94.     } else {
  95.         boxCtl = *GetWControls(previewWindowPtr);    /* get the control handle from the window record */
  96.         boxRect = &(boxCtl->ctlRect);    /* make a pointer to the control's rect */
  97.         
  98.         *xStart = ((long) (boxRect->h1 - XINSET) * 1200) / hPPI;
  99.         *xEnd   = ((long) (boxRect->h2 - XINSET) * 1200) / hPPI;
  100.         *yStart = ((long) (boxRect->v1 - YINSET) * 1200) / vPPI;
  101.         *yEnd   = ((long) (boxRect->v2 - YINSET) * 1200) / vPPI;
  102.     }
  103.     
  104.     if(*yEnd < *yStart) {        /* make sure that y is in the increasing direction */
  105.         temp = *yEnd;
  106.         *yEnd = *yStart;
  107.         *yStart = temp;
  108.         }
  109.  
  110.     if(*xEnd < *xStart){        /* make sure that x is in the increasing direction */
  111.         temp = *xEnd;
  112.         *xEnd = *xStart;
  113.         *xStart = temp;
  114.         } 
  115.             
  116.     return (*yEnd - *yStart);    /* scanlength */
  117. }
  118.  
  119. long doFixXPos( xStart,xEnd,yStart,yEnd,xres)
  120. unsigned long    *xStart,*xEnd,*yStart,*yEnd;
  121. word    xres;
  122. {
  123.     unsigned long    chunkiness, fixAmount;
  124.     unsigned long   remainder, diff, deltaX;
  125.  
  126.     diff = (Long)(*xEnd - *xStart);
  127.     chunkiness = 8*xres;
  128.     deltaX = diff%chunkiness;
  129.     fixAmount = chunkiness - deltaX;
  130.     
  131.     if(deltaX != 0) {
  132.         if(*xEnd + fixAmount > maxX) {
  133.             if(fixAmount >= *xStart) {
  134.                 *xStart = 0;
  135.                 *xEnd = maxX;
  136.             } else {
  137.                 *xStart -= fixAmount;
  138.             }
  139.         } else {
  140.             *xEnd += fixAmount;
  141.         }
  142.     }
  143.  
  144.     return(*xEnd - *xStart);    /* return scanWidth */
  145.  
  146. }
  147.  
  148.  
  149. doGetBrightness()
  150. {
  151.     return(GetCtlValue(GetCtlHandleFromID(NULL,BrightnessScroll)));    
  152. }
  153.  
  154. doGetContrast()
  155. {
  156.     return(GetCtlValue(GetCtlHandleFromID(NULL,ContrastScroll)));    
  157. }
  158.  
  159. doGetThreshold()
  160. {
  161.     return(GetCtlValue(GetCtlHandleFromID(NULL,ThresholdScroll)));    
  162. }
  163.  
  164. doGetImageComposition()
  165. {
  166.     return(GetCtlValue(GetCtlHandleFromID(NULL,CompositionPopUp))-1);    
  167. }
  168.  
  169. doGetBitsPerPixel(scanType)
  170. byte scanType;
  171. {
  172.     byte dpi;
  173.  
  174.     switch(scanType){
  175.         case 0:
  176.             dpi = 1;
  177.             break;
  178.         case 1:
  179.             dpi = 1;
  180.             break;
  181.         case 2:
  182.             dpi = 4;
  183.             break;
  184.         }
  185.     return(dpi);
  186. }
  187.  
  188. #if 0    /* currently not used, but will be when Data Chaining is implemented */
  189. static void invertImage(bufptr, buflen)
  190. unsigned    *bufptr;
  191. long         buflen;    /* number of 16-bit words in buffer */
  192. {
  193.     GrafPortPtr    theWindow, oldPort;
  194.     char        *statText;
  195.     unsigned    cursNum = 0;
  196.     unsigned    counter = 0;
  197.     word        oldSCB;
  198.     int            thermX, thermY;
  199.     int            chunkSize, chunkCount;
  200.     static Rect thermRect = {50,10,60,229};
  201.  
  202.     statText = "Now inverting the image";
  203.     
  204.     /* remember the current GrafPort */
  205.     oldPort = GetPort();
  206.     
  207.     /* draw the thermometer window */
  208.     theWindow = NewWindow2(NULL,NULL,NULL,NULL,2,ThermWindow,0x800E);
  209.     SetPort(theWindow);
  210.  
  211.     MoveTo((240 - CStringWidth(statText)) >> 1 ,30);
  212.     DrawCString(statText);
  213.  
  214.     FrameRect(&thermRect);    /* draw the thermometer frame */
  215.  
  216.     SetSolidPenPat(7);    /* use a red pen */
  217.     thermX = 11; thermY = 51;    /* init drawing locations */
  218.     chunkSize = buflen / 217;    /* determine size to process before filling in a line */
  219.     chunkCount = 0;
  220.  
  221.     SetMasterSCB((oldSCB = GetMasterSCB()) | 0x0100);    /* make sure cursor doesn't flicker */
  222.  
  223.     do {
  224.         *bufptr ^= 0xffff;
  225.         ++bufptr;
  226.         if(!(++counter & 0x1fff)) {
  227.             SetCursor(animCursor[++cursNum]);
  228.             if(!(cursNum < 7))    /* in other words, if cursNum >= 7 (but this produces better code) */
  229.                 cursNum = -1;
  230.         }
  231.         if(++chunkCount == chunkSize) {
  232.             MoveTo(thermX++, thermY);    /* init the pen position */
  233.             Line(0,7);
  234.             chunkCount = 0;
  235.         }
  236.     } while(--buflen);
  237.  
  238.     SetMasterSCB(oldSCB);    /* reset master SCB */
  239.     SetPort(oldPort);        /* reset the GrafPort */
  240.     CloseWindow(theWindow);    /* close the alert window */
  241.     SetSolidPenPat(0);        /* reset the pen back to black */
  242. }
  243. #endif
  244.  
  245. void doScanButton()
  246. {
  247.     unsigned        *tempPtr;
  248.     Handle          bufferHandle;
  249.     unsigned long    bufferSize, i, transferCount;
  250.     word             xGSres, yGSres;
  251.     unsigned long     scanLength,scanWidth;
  252.     unsigned long     xStart,xEnd,yStart,yEnd;
  253.     unsigned long     dataWidth, dataLength;
  254.     extern long     doScan();
  255.  
  256.     sWindowDef.windDescBlockLenMSB = 0;
  257.     sWindowDef.windDescBlockLenLSB = 0x28;
  258.     sWindowDef.windowIdentifier = 0;
  259.  
  260.     xGSres = getResolutionPopUp(GetCtlHandleFromID(NULL,XResolutionPopUp));
  261.     yGSres = getResolutionPopUp(GetCtlHandleFromID(NULL,YResolutionPopUp));
  262.     sWindowDef.xResolution  = SwapBytes(xGSres);    /* we want the # of 1200ths of an inch in lsb>>msb */
  263.     sWindowDef.yResolution  = SwapBytes(yGSres);    
  264.  
  265.     scanLength = doGetXYPos(&xStart,&xEnd,&yStart,&yEnd);
  266.     scanWidth = doFixXPos(&xStart,&xEnd,&yStart,&yEnd,xGSres);
  267.      sWindowDef.xUpperLeft = SwapWords(xStart);  
  268.     sWindowDef.yUpperLeft = SwapWords(yStart);
  269.     sWindowDef.width = SwapWords(scanWidth);
  270.     sWindowDef.length = SwapWords(scanLength);
  271.     sWindowDef.brightness = doGetBrightness();
  272.     sWindowDef.threshold = doGetThreshold();
  273.     sWindowDef.contrast = doGetContrast();
  274.     sWindowDef.imageComposition = doGetImageComposition();
  275.     sWindowDef.bitsPerPixel = doGetBitsPerPixel(sWindowDef.imageComposition);
  276.     sWindowDef.halfTone = SwapBytes(2); 
  277.     sWindowDef.paddingType = 3;
  278.  
  279.     switch(xGSres){
  280.         case 75:
  281.             scanWidth += 16;
  282.             break;
  283.         case 150:
  284.             scanWidth += 8;
  285.             break;
  286.     }
  287.  
  288.     dataWidth = (((scanWidth*xGSres)/1200) * (long) (sWindowDef.bitsPerPixel))/8;    /* in bytes */
  289.     dataLength = (scanLength*yGSres)/1200;    /* # of scan lines */
  290.             
  291.     bufferSize = dataWidth * dataLength;    /* bytes per scan line * # of scan lines */
  292.  
  293.     bufferHandle = NewHandle(bufferSize,_ownerid,attrFixed,NULL); 
  294.     if(_toolErr != 0) {
  295.             AlertWindow(4,NULL,(long) noMemAvailable);
  296.     } else {
  297.         WaitCursor();
  298.         transferCount = doScan(bufferHandle, bufferSize, &sWindowDef, dataWidth, dataLength);
  299.  
  300.         doOpenImage(dataWidth, dataLength, bufferHandle);
  301.         InitCursor();
  302.     }
  303.  
  304. }
  305. void doPreviewButton(){
  306. /*
  307.     format_call();
  308.     eject_call();
  309.     set_confg_parms();
  310.     set_format();
  311.     Assign_part();
  312.     arm_signal();
  313.     disarm_signal();
  314.     set_part_map();
  315.     reserve_unit();
  316.     release_unit();
  317.     send_diagnostics();
  318.     
  319.     return_dev_status();
  320.     return_confg_parms();
  321.     return_wait();
  322.     return_format();
  323.     return_part_map();
  324.     return_last_cmd();
  325.     
  326.     test_unit_rdy();
  327.     request_sense();
  328.     inquiry();
  329.     mode_sense();
  330.     get_wndow_parms();
  331.     extended_read();
  332.     get_data_status();
  333.     
  334.     if(_toolErr != 0) {
  335.         AlertWindow(4,NULL,(long) noMemAvailable);
  336.     }    
  337. */
  338. }
  339.